var gulp = require('gulp');
var less = require('gulp-less');
var path = require('path');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');
var browserSync = require('browser-sync').create();


// 编译 LESS 文件
gulp.task('build:bootstrap', function () {
  return gulp.src('./web/assets/v2/bootstrap/less/bootstrap-orange.less')
    .pipe(less({
      paths: [ path.join(__dirname, 'less', 'includes') ]
    }))
    .pipe(minifyCSS())
    .pipe(gulp.dest('./web/assets/v2/bootstrap/css'));
});

gulp.task('build:main', function () {
  return gulp.src(['./web/assets/v2/less/main-orange.less', './web/assets/v2/less/header-orange.less'])
    .pipe(less({
      paths: [ path.join(__dirname, 'less', 'includes') ]
    }))
    .pipe(minifyCSS())
    .pipe(gulp.dest('./web/assets/v2/css'));
});

gulp.task('less', ['build:bootstrap', 'build:main'] , function () {
  console.log('LESS 文件已编译完毕！');
})


// 编译 Sass
gulp.task('sass', function () {
  return gulp.src('./web/themes/jianmo/sass/8000wei.scss')
    .pipe(sass({
            outputStyle: 'expanded'
    }).on('error', sass.logError))
    .pipe(autoprefixer({
            browsers: ['last 2 versions', 'not ie < 8'],
            cascade: false
    }))
    .pipe(gulp.dest('./web/themes/jianmo/css'));
});

gulp.task('sass:watch', function () {
  gulp.watch('./web/themes/jianmo/sass/**/*.scss', ['sass']);
});


gulp.task('sass:dist', function () {
  return gulp.src('./web/themes/jianmo/sass/8000wei.scss')
    .pipe(sass({
            outputStyle: 'compressed'
    }).on('error', sass.logError))
    .pipe(autoprefixer({
            browsers: ['last 2 versions', 'not ie < 8'],
            cascade: false
    }))
    .pipe(minifyCSS())
    .pipe(gulp.dest('./web/themes/jianmo/css'));
});


// 监听后端页面
gulp.task('serve', function() {
    browserSync.init({
        proxy: "http://localhost/baqianwei/web/app_dev.php"
    });

    gulp.watch('./src/Topxia/WebBundle/**/*.twig').on('change', browserSync.reload);
    gulp.watch('./src/**/*.php').on('change', browserSync.reload);
});


// 监听前端页面
gulp.task('go', function() {
    browserSync.init({
        proxy: "http://localhost/baqianwei/web/app_dev.php"
    });

    gulp.watch('./web/**/*.twig').on('change', browserSync.reload);
    gulp.watch('./src/**/*.twig').on('change', browserSync.reload);
    gulp.watch('./web/**/jianmo/css/*.css').on('change', browserSync.reload);
});


// 默认 Gulp 任务
gulp.task('default', ['go', 'sass', 'sass:watch'], function () {
  console.log('项目已启动！');
});
